<template>
  <el-dialog
      :visible.sync="dialogVisible"
      width="700px"
      :before-close="closeDialog"
      append-to-body
      title="驳回"
  >


    <el-form  :label-position="labelPosition"  label-width="100px" ref="form" :model="rejectParam"  v-loading="loading">

      <el-form-item prop="nextNodes" label="后续任务">
        <el-select v-model="rejectParam.rejectNodId" @change="selectRejectNode"  placeholder="请选择" style="width: 400px">
          <el-option
              v-for="item in rejectNodes"
              :key="item.nodeId"
              :label="item.nodeName"
              :value="item.nodeId">
          </el-option>
        </el-select>
      </el-form-item>

      <el-form-item  label="审批人" >
        <el-select v-model="rejectParam.rejectTaskIds" multiple   style="width: 400px" :disabled="true">
          <el-option
              v-for="item in rejectParam.taskUsers"
              :key="item.taskId"
              :label="item.username"
              :value="item.taskId">
          </el-option>
        </el-select>
      </el-form-item>

      <el-form-item prop="icon" label="意见">
        <el-input size= "small" type="textarea"  placeholder="请输入意见" rows="6"  v-model="rejectParam.suggest" style="width: 400px"></el-input>
      </el-form-item>

    </el-form>
    <div class="btn-group">
      <el-button type="text" size="small" plain @click="closeDialog">取消</el-button>
      <el-button type="primary"  size="small" :disabled="loading" @click="reject">确定</el-button>
    </div>
  </el-dialog>
</template>

<script>
import {reject,getRejectNodeList} from '@/api/workflowInstance'
export default {
  components: {},
  data() {
    return {
      labelPosition: 'left',
      dialogVisible: false,
      loading: false,
      workflowDetail:{},
      rejectNodes:[],
      selectFlag:false,
      selectMoreFlag:false,
      rejectParam:this.defaultRejectParam(),
      rejectNodeParam:this.defaultRejectNodeParam()
    }
  },
  created() {
  },
  methods: {
    defaultRejectNodeParam(){
      return {
        processId:"",
        processInstanceId:"",
        taskId:"",
        account:""
      }
    },
    defaultRejectParam(){
      return {
        processId:"",
        processInstanceId:"",
        taskId:"",
        account:"",
        rejectNodId:"",
        suggest:"",
        taskUsers:[],
        rejectTaskIds:[]
      }
    },
    selectRejectNode(row){
      let index =  this.rejectNodes.findIndex(node=>node.nodeId === row);
      this.rejectParam.taskUsers = this.rejectNodes[index].users
      let taskIds = [];
      this.rejectParam.taskUsers.forEach(user=>{
        taskIds.push(user.taskId);
      })
      this.rejectParam.rejectTaskIds = taskIds;
    },
    async reject(){
      if(this.rejectParam.rejectNodId == null || this.rejectParam.rejectNodId === '' ||
          this.rejectParam.rejectTaskIds == null || this.rejectParam.rejectTaskIds.length === 0){
        this.$message.warning("请选择退回节点");
        return;
      }
      if(this.rejectParam.suggest == null || this.rejectParam.suggest === ""){
        this.$message.warning("请填写意见");
        return;
      }
      this.loading = true;
      this.rejectParam.processInstanceId = this.workflowDetail.processInstanceId;
      this.rejectParam.processId =  this.workflowDetail.details.processId;
      this.rejectParam.taskId = this.workflowDetail.details.taskId;
      let ret = await reject(this.rejectParam);
      if(ret.code === '200'){
        this.$message.success("驳回成功");
        this.loading = false;
        location.reload();
      }
      this.loading = false;
    },
    async getRejectTask(){
      this.rejectNodeParam.processId = this.workflowDetail.details.processId;
      this.rejectNodeParam.processInstanceId = this.workflowDetail.details.processInstanceId;
      this.rejectNodeParam.taskId = this.workflowDetail.details.taskId;
      let ret = await getRejectNodeList(this.rejectNodeParam);
      this.rejectNodes = ret.data;
    },
    showDialog(row) {
      this.rejectParam = this.defaultRejectParam();
      this.rejectNodeParam = this.defaultRejectNodeParam();
      this.workflowDetail = row;
      this.getRejectTask();
      this.dialogVisible = true
    },
    closeDialog() {
      this.dialogVisible = false
      this.loading = false
      this.rejectParam = this.defaultRejectParam();
      this.rejectNodeParam = this.defaultRejectNodeParam();
    },
  },

}
</script>


<style src="../../css/index.scss" lang="scss"></style>
<style lang="scss" scoped>
.search-item {
  float: left;
  margin: 0 20px 20px 0;

  span {
    color: #606266;
    font-size: 14px;
    margin-right: 0px;
  }
}
.btn-group{
  display: flex;
  justify-content: right;
  .el-button{
    width: 50px;
  }
}


.workProcessClass {
  height: 500px;
  overflow: auto;
  border: 1px solid #ebeef5;
  border-radius: 4px;
  background: white;
}
</style>
